package microcredit.model.util;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;

public class ModelUtils {

	public static Date createTimestamp() {
		return new Timestamp(System.currentTimeMillis());
	}
	
	public static String formatID(Long id, int size) {
		StringBuilder b = new StringBuilder(String.valueOf(id));
		int count = size - b.length();
		for (int i = 0; i < count; i++) {
			b.insert(0, "0");
		}
		return b.toString();
	}
	
	public static <E extends Comparable<E>> int compare(E o1, E o2) {
		if (o1 == null && o2 == null) {
			return 0;
		} else if (o1 == null){
			return -1;
		} else if (o2 == null) {
			return 1;
		}
		return o1.compareTo(o2);
	}
	
	public static boolean equals(Object o1, Object o2) {
		if (o1 == null && o2 == null) {
			return true;
		} else if (o1 == null || o2 == null) {
			return false;
		} else {
			return (o1.equals(o2));
		}
	}
	public static BigDecimal subtract(BigDecimal d1, BigDecimal d2) {
		if (d1 == null) {
			d1 = new BigDecimal(0);
		}
		if (d2 == null) {
			d2 = new BigDecimal(0);
		}
		return d1.subtract(d2);
	}

}
